package com.fesine.servlet;

import com.fesine.util.FileUtil;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.util.List;

/**
 * Created by Fesine on 2016/6/14.
 */
public class FileServlet extends HttpServlet {

    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        System.out.println("fileServlet do post");
        String filePath = request.getParameter("filePath");
        List<String> fileList = FileUtil.getFileName(filePath);
        if (fileList != null) {
            HttpSession session = request.getSession();
            session.setAttribute("filePath", filePath);
            request.setAttribute("fileList", fileList);
            request.setAttribute("fileListSize", fileList.size());
            request.setAttribute("info", "文件路径：" + filePath);
            request.setAttribute("filePath", filePath);
            request.getRequestDispatcher("fileList.jsp").forward(request, response);//请求转发
        } else {
            request.setAttribute("info", "文件路径不存在或路径下无文件：" + filePath);
            request.getRequestDispatcher("fileList.jsp").forward(request, response);//请求转发
        }
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        System.out.println("fileServlet do get");
        doPost(request,response);
    }
}
